package com.china08.yxyapi.api.schwebsite.index;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 
 * @annotation: 最新资讯
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController("SwsIndexNewArticleSrhController")
public class SwsIndexNewArticleSrhController extends YxyApiBaseController {

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @Autowired
    private SwsArticleRepository swsArticleRepository;

    @RequestMapping(value = "/api_yw/schwebsite/index/new/article/sch", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String domainName = getDomainName(request);
        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains byDomainName = swsSettingDomainsRepository.findByDomainName(domainName);
        Assert.isTrue(null != byDomainName, "域名对象为空");
        String schoolId = byDomainName.getSchoolId();

        List<SwsArticle> articles = swsArticleRepository
                .findTop5ByOwnerSchoolIdAndAuditStatusOrderByPublishDateDesc(schoolId, 1);

        List<RespModel> respModels = new ArrayList<SwsIndexNewArticleSrhController.RespModel>();
        if (articles != null && articles.size() > 0) {
            for (SwsArticle swsArticle : articles) {
                RespModel model = new RespModel();
                model.content = Jsoup.parse(swsArticle.getTitle()).text();
                model.id = swsArticle.getId();
                model.section1Id = swsArticle.getSection1Id();
                if (StringUtils.isBlank(swsArticle.getSection2Id())) {
                    model.section2Id = "0";
                } else {
                    model.section2Id = swsArticle.getSection2Id();
                }
                model.showDate = DateToolUtils.format(swsArticle.getPublishDate(), "MM-dd");
                respModels.add(model);
            }
        }
        return respModels;
    }

    protected static class ReqModel extends YxyApiReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {

        public String id;

        public String content;

        public String showDate;

        public String section1Id;

        public String section2Id;

    }

}
